import 'package:honeypotflutter/net/public_http.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_modal.dart';
import '../model/deliver_boxInfo_model.dart';
import '../model/deliver_orderInfo_model.dart';
import '../model/deliver_param.dart';
import '../model/deliver_result.dart';

class DeliverApi {
  /*
  * 柜机详情
  * */
  static Future<DeliverBoxInfoModel> boxInfo(String qr) async {
    Map<String, dynamic> param = {"qrUrlString": qr};
    try {
      Map<String, dynamic> result = await MGHttp.post(
        "api/services/app/DeliveryPackage/QueryEmptyCabinetWithQrUrlAsync",
        params: param,
        progress: "数据加载中...",
      );
      DeliverBoxInfoModel model = DeliverBoxInfoModel.fromJson(result);
      return model;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 定位
  * */
  static Future<bool> location({int? deviceId, double? lat, double? lng}) async {
    Map<String, dynamic> param = {
      "deviceId": deviceId,
      "lat": lat,
      "lng": lng,
    };
    try {
      Map<String, dynamic> result = await MGHttp.post(
        "services/app/devices/CreateDeviceLocation",
        params: param,
      );
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 订单信息
  * */
  static Future<DeliverOrderInfoModel> orderInfo({String? expressNo, String? disPlayNo, int? deviceId}) async {
    Map<String, dynamic> param = {
      "DisableVagueMatch": true,
      "expressNo": expressNo,
      "deviceId": deviceId,
      "disPlayNo": disPlayNo,
    };
    try {
      Map<String, dynamic> result = await MGHttp.get(
        "api/services/app/ExpressNo/GetPhoneNoByPattern",
        params: param,
        progress: "面单信息获取中...",
      );
      DeliverOrderInfoModel model = DeliverOrderInfoModel.fromJson(result);
      return model;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 设置用户快递公司
  * */
  static Future<bool> setUserCourierCompany(List list) async {
    try {
      Map param = {"companyIds": list};
      await MGHttp.post("api/services/app/Account/ChangeUserCourierCompanySettingForApple", params: param, progress: "修改中...");
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 投递
  * */
  static Future<DeliverResult> deliver(DeliverParam param) async {
    try {
      Map<String, dynamic> newParam = {
        "deviceId": param.deviceId,
        "doorType": param.doorType,
        "orderPhone": param.orderPhone,
        "expressNo": param.expressNo,
        "expressNoType": param.expressNoType,
        "sortingModel": param.sortingModel,
        "businessFormType": param.businessFormType,
      };
      if (param.companyCode != null) {
        newParam["courierCompanyCode"] = param.companyCode;
      }
      Map<String, dynamic> result = await MGHttp.post(
        "api/services/app/Delivery/Delivery",
        params: newParam,
        progress: "投递中...",
      );
      DeliverResult resultModel = DeliverResult.fromJson(result);
      return resultModel;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 确认/取消
  * */
  static Future<bool> sureAction({String? id, int? status, Map? customParam}) async {
    try {
      Map param = {
        "id": id,
        "confirmState": status,
      };
      if (customParam != null) {
        param.addAll(customParam);
      }

      await MGHttp.post("services/app/Delivery/DeliveryConfirm", params: param, progress: (status == 1) ? "确认中..." : "取消中...");
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 再次开门
  * */
  static Future<DeliverResult> againOpenDoor(String id) async {
    try {
      Map<String, dynamic> param = {"id": id};
      Map<String, dynamic> result = await MGHttp.post("services/app/Delivery/ReOpenDoor", params: param, progress: "重新开门中...");
      DeliverResult resultModel = DeliverResult.fromJson(result);
      return resultModel;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }
}
